package com.bci.pwtz.service.impl;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.BusinessRefMapper;
import com.bci.pwtz.mysql.model.BusinessRef;
import com.bci.pwtz.service.BusinessRefService;
/**
 * 
 * 主体数据工具类 <br/>
 * <p>Description:<br/>
 *    <p>
 * <p>For Examples <br/>
 * <PRE> </PRE>
 * <p>
 */
@Service
public class BusinessRefServiceImpl implements BusinessRefService
{
    private static final Logger _logger = Logger.getLogger(BusinessRefServiceImpl.class);
    private JdbcTemplate jdbcTemplate;
    private BusinessRefMapper businessRefMapper;
    
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
    {
        this.jdbcTemplate = jdbcTemplate;
    }
    

    public void setBusinessRefMapper(BusinessRefMapper businessRefMapper)
    {
        this.businessRefMapper = businessRefMapper;
    }
    
    @Override
    public List<BusinessRef> getBusinessRefList() throws PwtzDatabaseException
    {
        List<BusinessRef> list = null;
        try
        {
            list = businessRefMapper.selectAll();
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list != null)
        {
            _logger.debug("findDictionary:result menu=" + list.toString());
        }
        return list;
    }

    @Override
    public boolean canDeleteWithoutFlag(String deleteTableName,String pkValue)
    {
        if(PwtzConstants.BUSINESS_REF_CACHE == null)
            return true;
        
        String bTableName = "";
        String bRefFieldName = "";
        String bOtherCondition = "";
        String checkSql = "";
        for(BusinessRef ref:PwtzConstants.BUSINESS_REF_CACHE)
        {
            if(ref.getFlag()!=null && !ref.getFlag().equals("0"))
                continue;
            if(ref.getaTableName().equals(deleteTableName))
            {
                bTableName = ref.getbTableName();
                bRefFieldName = ref.getbRefFieldName();
                bOtherCondition = ref.getbOtherCondition();
                checkSql = "SELECT 1 AS FLAG FROM DUAL WHERE EXISTS(SELECT 1 FROM {0} WHERE {1}=''{2}'' AND {3})";
                checkSql = MessageFormat.format(checkSql,bTableName,bRefFieldName,pkValue,bOtherCondition);
                if(_logger.isDebugEnabled())
                {
                    _logger.debug("canDeleteWithoutFlag判断业务删除依赖语句,SQL="+checkSql);
                }
                List<Map<String,Object>> retVal = jdbcTemplate.queryForList(checkSql);
                if(retVal.size()!=0)
                    return false;                
            }
        }
        return true;   
        
    }
    @Override
    public boolean canDelete(String deleteTableName, String pkValue)
    {
        if(PwtzConstants.BUSINESS_REF_CACHE == null)
            return true;

        String bTableName = "";
        String bRefFieldName = "";
        String bOtherCondition = "";
        String checkSql = "";
        for(BusinessRef ref:PwtzConstants.BUSINESS_REF_CACHE)
        {
            if(ref.getaTableName().equals(deleteTableName))
            {
                bTableName = ref.getbTableName();
                bRefFieldName = ref.getbRefFieldName();
                bOtherCondition = ref.getbOtherCondition();
                if(ref.getRefId()==29||ref.getRefId()==101){
                    checkSql = "SELECT 1 AS FLAG FROM DUAL WHERE EXISTS(SELECT 1 FROM {0} WHERE {1}=''{2}'' AND DeviceID!=''{3}'' AND {4})";
                    checkSql = MessageFormat.format(checkSql,bTableName,bRefFieldName,pkValue,pkValue,bOtherCondition);
                }else{
                    checkSql = "SELECT 1 AS FLAG FROM DUAL WHERE EXISTS(SELECT 1 FROM {0} WHERE {1}=''{2}'' AND {3})";
                    checkSql = MessageFormat.format(checkSql,bTableName,bRefFieldName,pkValue,bOtherCondition);
                }
                if(_logger.isDebugEnabled())
                {
                    _logger.debug("canDelete判断业务删除依赖语句,SQL="+checkSql);
                }
                List<Map<String,Object>> retVal = jdbcTemplate.queryForList(checkSql);
                if(retVal.size()!=0)
                    return false;                
            }
        }
        return true;        
    }

    
}
